package com.easy.user.controller;

import com.easy.common.util.ImageUtil;
import com.easy.common.util.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
//上传图片
@RestController
@RequestMapping("/user/upload")
@CrossOrigin//允许跨域
public class UploadController {
    @RequestMapping("/file")
    public Result upload(@RequestParam("file") MultipartFile file){
        try {
            //获取上传的图片输入流
            InputStream is=file.getInputStream();
            //获取上传的图片名称
            String filename=file.getOriginalFilename();
            //调用ImageUtil工具类进行文件上传，并获取新文件名
            String newFileName= ImageUtil.upload(is,filename);
            return Result.success("图片上传成功",newFileName);
        } catch (IOException e) {
            throw new RuntimeException("图片上传失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
